<template>
  <div>
    <div class="filter-container">
      <div class="filter-item">
        <span>字典标签或值:</span>
        <el-input v-model="listQuery.keyword" clearable placeholder="请输入字典标签或值" @keyup.enter="handleFilter" />
      </div>
      <el-button v-permission="['sys:dict:list']" class="filter-item" type="primary" icon="Search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" text type="primary" icon="Delete" @click="clearQuery">清除条件</el-button>
    </div>
    <div class="operationList">
      <div>
        <el-button v-permission="['sys:dict:add']" text type="primary" icon="Plus" @click="handleCreate">新增</el-button>
      </div>
      <div>
        <el-button text type="primary" icon="RefreshRight" @click="handleFilter">刷新</el-button>
      </div>
    </div>

    <el-table v-loading="listLoading" border :data="list" :row-class-name="tableRowClass" fit highlight-current-row header-cell-class-name="header-cell" >
      <el-table-column label="序号" width="100" type="index" />
      <el-table-column prop="label" label="字典标签" width="160" />
      <el-table-column prop="value" label="字典值" width="160" />
      <el-table-column prop="remarks" label="备注" width="160" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button v-permission="['sys:dict:update']" size="small" plain type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="['sys:dict:delete']" size="small" plain type="danger" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" v-model:page="listQuery.page" v-model:limit="listQuery.limit" :total="total" :page-sizes="pageArray" @pagination="getList" />

    <dictForm ref="dictForm" @refresh-list="getList"/>
  </div>
</template>

<script>
import { fetchDictList, deleteDict } from '@/api/sys/dict/dict'
import Pagination from '@/components/Pagination/index.vue'
import dictForm from './dictForm.vue'
export default {
  name: 'SysDictComponent',
  components: { Pagination, dictForm },
  data() {
    return {
      dictGroup: {},
      list: null,
      total: 0,
      listLoading: false,
      pageArray: this.$store.dictStore.pageArray,
      listQuery: {
        page: 1,
        limit: this.$store.dictStore.defaultPageSize,
        importance: undefined,
        title: undefined,
        type: undefined
      },
      showReviewer: false,
      temp: {
        id: undefined,
        label: '',
        value: '',
        sort: 1,
        remarks: ''
      },
      loading: false,
      dialogFormVisible: false,
      title: undefined,
      rules: {
        label: [{ required: true, message: '字典标签不能为空' }],
        value: [{ required: true, message: '字典值不能为空' }],
        sort: [{ required: true, message: '排序不能为空' }]
      },
      downloadLoading: false
    }
  },
  methods: {

    /**
     * 获取字典列表
     */
    getList(loading = true) {
      this.listLoading = loading
      fetchDictList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    /**
     * 修改状态
     * @param row 选中数据
     * @param status 状态
     */
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    /**
     * 重置表单
     */
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
        label: '',
        value: '',
        sort: 1,
        remarks: '',
        gid: this.dictGroup.id
      }
    },
    /**
     *
     * @param row 选中数据
     */
    refreshGroupDict(row) {
      this.listQuery.gid = row.id
      this.listQuery.page = 1
      this.listQuery.keyword = ''
      this.dictGroup = row
      this.$refs.dictForm.setdictGroup(row)
      this.getList()
    },
    /**
     * 新增
     */
    handleCreate() {
      if (this.dictGroup.id === undefined || this.dictGroup.id === '') {
        this.$message.error('请选择字典分组')
      } else {

        this.$refs.dictForm.handleCreate()
      }
    },
    /**
     * 编辑
     * @param row 选中数据
     */
    handleUpdate(row) {
      this.$refs.dictForm.handleUpdate(row)
    },
    /**
     * 删除
     * @param row 选中数据
     */
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.$delAnim = true

        deleteDict([row.id]).then((response) => {
          const data = response.data
          if (data.success) {
            row.$delAnim = true
            this.$store.dictStore.initDict()
            this.dialogFormVisible = false
            this.$message.success('删除成功')
            this.getList(false)
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
